home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / XLIB06.ZIP / XLIBTL02 / SHOWPAL.C < prev    next >
Text File  |  1993-04-10  |  9KB  |  305 lines

  1. #include <stdio.h>
  2. #include <alloc.h>
  3. #include <mem.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <dir.h>
  7. #include <dos.h>
  8. #include <math.h>
  9. #include <values.h>
  10. #include "xlib_all.h"
  11.  
  12. #define BUFF_LEN 100
  13.  
  14.    char drive[MAXDRIVE];
  15.    char dir[MAXDIR];
  16.    char name[MAXFILE],newname[MAXFILE];
  17.    char ext[MAXEXT];
  18.    char filenamewild[100];
  19.    char filenamein[100];
  20.    char filenameout[100];
  21.    char filenamedif[100];
  22.    unsigned int currvalue,maxpal=0,minpal=MAXINT;
  23.    int maxpalid=0,minpalid=0;
  24.    char reverse_text[256];
  25.    int index[256];
  26.    int brightness[256];
  27.    long color_order[256];
  28.  
  29. typedef struct _palstruc {
  30.   unsigned char r;
  31.   unsigned char g;
  32.   unsigned char b;
  33. } far * PAL_STRUC;
  34.  
  35. char *intro[9]= {"This program is a simple palette manipulation and display program.\n",
  36.          "As well a displaying the pallete it has several sort and move     \n",
  37.          "Options for palette entries. Palette entries can be moved using   \n",
  38.          "the mouse.. Just click and drag. A new palette may be written to  \n",
  39.          "disk along with a \".DIF\" file which contains the differences in \n",
  40.          "palette entries between the original and new file which could be  \n",
  41.          "used to modify existing bitmaps.\n",
  42.          NULL};
  43.  
  44.  
  45. PAL_STRUC p;
  46.  
  47. void load_user_fonts();
  48.  
  49. int brightcompare(const void *a,const void *b){
  50.   return brightness[*((int*)b)]-brightness[*((int*)a)];
  51. }
  52. int ordercompare(const void *a,const void *b){
  53.   return color_order[*((int*)b)]-color_order[*((int*)a)];
  54. }
  55.  
  56. void syntax(){
  57.   int i;
  58.   printf("SHOWPAL - Palette display and manipulation utility          T. Gouthas 20/11/92\n");
  59.   printf("\n SHOWPAL <pal filespec> \n\n");
  60.   for(i=0;i<9 && intro[i];i++)
  61.     printf(intro[i]);
  62.   exit(0);
  63. };
  64.  
  65. size_t farfread(void far *destptr,size_t size, size_t n, FILE *fp){
  66.    unsigned len;
  67.    unsigned char buff[BUFF_LEN];
  68.    len=size*n;
  69.    while(len >=BUFF_LEN){
  70.      fread(buff,BUFF_LEN,1,fp);
  71.      movedata((unsigned)_DS,(unsigned)buff,FP_SEG(destptr),FP_OFF(destptr),BUFF_LEN);
  72.      len-=BUFF_LEN;
  73.      (char far *)destptr+=BUFF_LEN;
  74.    }
  75.    if (len>0){
  76.      fread(buff,len,1,fp);
  77.      movedata((unsigned)_DS,(unsigned)buff,FP_SEG(destptr),FP_OFF(destptr),len);
  78.    }
  79. };
  80.  
  81. size_t farfwrite(void far *srcptr,size_t size, size_t n, FILE *fp){
  82.    WORD len;
  83.    BYTE buff[BUFF_LEN];
  84.    len=size*n;
  85.    while(len >=BUFF_LEN){
  86.      movedata(FP_SEG(srcptr),FP_OFF(srcptr),(unsigned)_DS,(unsigned)buff,BUFF_LEN);
  87.      fwrite(buff,BUFF_LEN,1,fp);
  88.      len-=BUFF_LEN;
  89.      (char far *)srcptr+=BUFF_LEN;
  90.    }
  91.    if (len>0){
  92.      movedata(FP_SEG(srcptr),FP_OFF(srcptr),(unsigned)_DS,(unsigned)buff,len);
  93.      fwrite(buff,len,1,fp);
  94.    }
  95. };
  96.  
  97.  
  98.  
  99.  
  100. int which_cell(int x, int y){
  101.   int xpos,ypos;
  102.   if (x < 0) return -1;
  103.   if (x > 255*23+22) return -1;
  104.   if (y < 18) return -1;
  105.   if (y > 255*29+28+18) return -1;
  106.   xpos =  x / 23;
  107.   ypos =  (y-18) / 29;
  108.   return ypos*16+xpos;
  109.  
  110. }
  111.  
  112. void write_outfiles(){
  113.   FILE *f;
  114.   int i;
  115.   f=fopen(filenameout,"wb");
  116.   if (!f) return;
  117.   for(i=0;i<256;i++)
  118.     farfwrite(&p[index[i]].r,3,1,f);
  119.   fclose(f);
  120.   f=fopen(filenamedif,"wt");
  121.   if (!f) return;
  122.   for(i=0;i<256;i++)
  123.     if (index[i]!=i)
  124.       fprintf(f,"%d %d\n",i,index[i]);
  125.   fclose(f);
  126. }
  127.  
  128. void main(int argc, char *argv[] ){
  129.   FILE *f;
  130.   struct ffblk ffb;
  131.   ldiv_t d;
  132.   unsigned char c[3];
  133.   
  134.   int i,j,k,l,infilesize;
  135.   char far *buff;
  136.   char ch;
  137.   int x0,y0,x1,y1,idx0,idx1;
  138.  
  139.   if (argc<2) syntax();
  140.   if (!strcmp(argv[1],"-g")){
  141.     if (argc<3) syntax();
  142.     strcpy(filenamewild,argv[2]);
  143.   } else strcpy(filenamewild,argv[1]);
  144.   fnsplit(filenamewild,drive,dir,NULL,ext);
  145.   if (*ext=='\0') strcat(filenamewild,".PAL");
  146.   if(findfirst(filenamewild,&ffb,0)){
  147.     return;
  148.   }
  149.   fnsplit(ffb.ff_name,NULL,NULL,name,ext);
  150.   fnmerge(filenamein,drive,dir,name,ext);
  151.   strcpy(newname,"NEWPAL");
  152.   fnmerge(filenameout,drive,dir,newname,ext);
  153.   fnmerge(filenamedif,drive,dir,newname,".dif");
  154.   f=fopen(filenamein,"rb");
  155.   if (!f) return;
  156.   fseek(f,0,SEEK_END);
  157.   infilesize = (unsigned) ftell(f);
  158.   fseek(f,0,SEEK_SET);
  159.   if (infilesize < 768) return;
  160.   buff=farmalloc(infilesize);
  161.   if (buff==NULL) return;
  162.   farfread(buff,infilesize,1,f);
  163.   fclose(f);
  164.   for (i=0;i<256;i++) index[i]=i;
  165.   x_set_mode(X_MODE_376x564,376/4);
  166.   x_text_init();
  167.   load_user_fonts();
  168.   x_mouse_init();
  169.   x_set_font(FONT_USER);
  170.   x_put_pal_raw(buff,256,0);
  171.   p=(PAL_STRUC) buff;
  172.   for (i=0;i<256;i++){
  173.      currvalue=(int)(p[i].r)+
  174.            (int)(p[i].g)+
  175.            (int)(p[i].b);
  176.      brightness[i]=(int)(p[i].r)*(int)(p[i].r)+
  177.            (int)(p[i].g)*(int)(p[i].g)+
  178.            (int)(p[i].b)*(int)(p[i].b);
  179.      color_order[i]= (((int)(p[i].r))<<16)+
  180.              (((int)(p[i].g))<<8)+
  181.              (int)(p[i].b);
  182.      if (currvalue<minpal){
  183.        minpal=currvalue;
  184.        minpalid=i;
  185.      } else if (currvalue>maxpal){
  186.        maxpal=currvalue;
  187.        maxpalid=i;
  188.      }
  189.   }
  190.   for (i=0;i<256;i++){
  191.      currvalue=(int)(p[i].r)+
  192.            (int)(p[i].g)+
  193.            (int)(p[i].b);
  194.      if (currvalue<maxpal/2)
  195.        reverse_text[i]=0;
  196.      else
  197.        reverse_text[i]=1;
  198.   }
  199.  
  200.   x_rect_fill(0,0,375,563,0,minpalid);
  201.   x_set_font(FONT_8x15);
  202.   x_printf(0,0,0,maxpalid,"%s%s",name,ext);
  203.   x_set_font(FONT_USER);
  204.   k=0;
  205.   for(j=0;j<16;j++)
  206.     for(i=0;i<16;k++,i++){
  207.       x_rect_fill(i*23,j*29+18,i*23+22,j*29+18+28,0,index[k]);
  208.       x_printf(i*23,j*29+18,0,reverse_text[index[k]]?minpalid:maxpalid,"%d",
  209.     index[k]);
  210.     }
  211.   x_set_font(FONT_8x15);
  212.   x_printf(0,507,0,maxpalid,"Sort by B)rightness C)olor or O)riginal order");
  213.   x_printf(0,519,0,maxpalid,"W)rite new pal and difference file Q)uit");
  214.   x_show_mouse();
  215.   MouseColor=maxpalid;
  216.   do {
  217.    do {} while (!kbhit() && !(MouseButtonStatus&LEFT_PRESSED));
  218.    if (kbhit()){
  219.      ch=getch();
  220.      if (ch=='@') ch=' ';
  221.    } else {
  222.       x0=MouseX; y0=MouseY;
  223.       ch='@';
  224.       do {} while (MouseButtonStatus&LEFT_PRESSED);
  225.       x1=MouseX; y1=MouseY;
  226.       idx0=which_cell(x0,y0);
  227.       if (idx0>-1){
  228.     idx1=which_cell(x1,y1);
  229.     if (idx1>-1){
  230.       i=index[idx0];
  231.       index[idx0]=index[idx1];
  232.       index[idx1]=i;
  233.     }
  234.       }
  235.    }
  236.    x_hide_mouse();
  237.    if (toupper(ch)=='B'){
  238.       qsort(index,256,2,brightcompare);
  239.    } else if (toupper(ch)=='C'){
  240.       qsort(index,256,2,ordercompare);
  241.    } else if (toupper(ch)=='O'){
  242.       for(i=0;i<256;i++) index[i]=i;
  243.    } else if (toupper(ch)=='W'){
  244.       write_outfiles();
  245.    }
  246.    if (toupper(ch)!='Q'&&toupper(ch)!='W'){
  247.      x_rect_fill(0,16,375,500,0,minpalid);
  248.      x_set_font(FONT_USER);
  249.      k=0;
  250.      for(j=0;j<16;j++)
  251.        for(i=0;i<16;k++,i++){
  252.      x_rect_fill(i*23,j*29+18,i*23+22,j*29+18+28,0,index[k]);
  253.      x_printf(i*23,j*29+18,0,reverse_text[index[k]]?minpalid:maxpalid,"%d",
  254.        index[k]);
  255.        }
  256.       x_show_mouse();
  257.   }
  258.   } while(toupper(ch)!='Q');
  259.   x_mouse_remove();
  260.   x_text_mode();
  261.   farfree(buff);
  262. }
  263. unsigned char fontdat[]={
  264.   30,0,8,0,0,0,12,30,63,63,0,0,7,0,0,63,63,30,12,0,0,7,0,0,0,0,0,0,0,0,4,
  265.   4,4,4,4,4,0,4,0,4,10,10,0,0,0,0,0,0,5,0,10,31,10,31,10,0,0,6,4,30,5,14,
  266.   20,15,4,0,6,0,35,19,8,4,50,49,0,7,6,9,5,2,21,9,22,0,6,6,4,2,0,0,0,0,0,
  267.   4,8,4,2,2,2,4,8,0,5,2,4,8,8,8,4,2,0,5,0,10,4,31,4,10,0,0,6,0,4,4,
  268.   31,4,4,0,0,6,0,0,0,0,0,6,4,2,4,0,0,0,31,0,0,0,0,6,0,0,0,0,0,0,2,
  269.   0,3,32,16,8,4,2,1,0,0,7,14,17,25,21,19,17,14,0,6,4,6,4,4,4,4,14,0,5,14,17,
  270.   16,14,1,17,31,0,6,14,17,16,12,16,17,14,0,6,1,9,9,9,31,8,8,8,6,31,1,15,16,16,17,
  271.   14,0,6,14,17,1,15,17,17,14,0,6,31,16,16,8,4,4,4,0,6,14,17,17,14,17,17,14,0,6,14,
  272.   17,17,30,16,17,14,0,6,0,4,4,0,0,4,4,0,4,0,4,4,0,0,4,4,2,4,8,4,2,1,2,
  273.   4,8,0,5,0,0,31,0,0,31,0,0,6,2,4,8,16,8,4,2,0,6,14,17,8,4,4,0,4,0,6,
  274.   14,17,21,29,13,1,30,0,6,4,10,17,17,31,17,17,0,6,15,17,17,15,17,17,15,0,6,14,17,1,1,
  275.   1,17,14,0,6,7,9,17,17,17,9,7,0,6,31,1,1,7,1,1,31,0,6,31,1,1,7,1,1,1,0,
  276.   6,14,17,1,25,17,17,14,0,6,17,17,17,31,17,17,17,0,6,14,4,4,4,4,4,14,0,5,16,16,16,
  277.   16,17,17,14,0,6,17,9,5,3,5,9,17,0,6,1,1,1,1,1,1,31,0,6,17,27,21,21,17,17,17,
  278.   0,6,17,19,21,25,17,17,17,0,6,14,17,17,17,17,17,14,0,6,15,17,17,15,1,1,1,0,6,14,17,
  279.   17,17,21,9,22,0,6,15,17,17,15,5,9,17,0,6,14,17,1,14,16,17,14,0,6,31,4,4,4,4,4,
  280.   4,0,6,17,17,17,17,17,17,14,0,6,17,17,17,17,10,10,4,0,6,17,17,17,21,21,27,17,0,6,17,
  281.   17,10,4,10,17,17,0,6,17,17,10,4,4,4,4,0,6,31,16,8,4,2,1,31,0,6,14,2,2,2,2,
  282.   2,14,0,5,0,1,2,4,8,16,0,0,6,14,8,8,8,8,8,14,0,5,4,10,17,0,0,0,0,0,6,
  283.   0,0,0,0,0,0,31,0,6,6,2,4,0,0,0,0,0,4,0,0,14,16,30,17,14,0,6,1,1,15,17,
  284.   17,17,15,0,6,0,0,14,17,1,17,14,0,6,16,16,30,17,17,17,30,0,6,0,0,14,17,15,1,14,0,
  285.   6,12,18,2,7,2,2,2,0,6,0,0,14,17,17,30,16,14,6,1,1,15,17,17,17,17,0,6,1,0,1,
  286.   1,1,9,6,0,5,16,0,16,16,16,16,18,12,6,1,1,17,9,7,9,17,0,6,1,1,1,1,1,1,6,
  287.   0,4,0,0,11,21,21,17,17,0,6,0,0,13,19,17,17,17,0,6,0,0,14,17,17,17,14,0,6,0,0,
  288.   15,17,17,15,1,1,6,0,0,14,17,17,30,16,16,6,0,0,13,19,1,1,1,0,6,0,0,30,1,14,16,
  289.   15,0,6,2,2,7,2,2,2,12,0,5,0,0,17,17,17,17,14,0,6,0,0,17,17,10,10,4,0,6,0,
  290.   0,17,17,21,27,17,0,6,0,0,17,10,4,10,17,0,6,0,0,17,17,18,28,17,14,6,0,0,31,8,4,
  291.   2,31,0,6,24,4,4,2,4,4,24,0,6,4,4,4,0,4,4,4,0,4,6,8,8,16,8,8,6,0,6,
  292.   0,0,10,5,0,0,0,0,5
  293. } ;
  294.  
  295. void load_user_fonts(){
  296.   static char far* newfnt;
  297.   int i; char c;
  298.   newfnt = MK_FP(_DS,fontdat);
  299.   x_text_init();
  300.   x_register_userfont(newfnt);
  301.   x_set_font(2);
  302. }
  303.  
  304.  
  305.